public with sharing class WeekPlanCreatedCampaignExtension {
    public Weekly_Plan__c wpc {get;set;}
    public Event t {get;set;}
    public String rtId{get;set;}
    public String rtHZId{get;set;}
    public String strjson {get;set;}
    public String tabUrl; 

    public WeekPlanCreatedCampaignExtension(ApexPages.StandardController con) {
        RecordType rtHZ=[select id from RecordType where DeveloperName='CamEvent'];
        RecordType rt=[select id from RecordType where DeveloperName='CamPlan'];
        
        
        rtId=rt.Id;
        rtHZId=rtHZ.Id;
        wpc=new Weekly_Plan__c();

        t=new Event();
        
        PageReference page=System.currentPageReference();
        Map<String,String> pageMap=page.getParameters();
        tabUrl = pageMap.get('retURL');
        if(tabUrl == null){
            tabUrl = '/' + Weekly_Plan__c.sObjectType.getDescribe().getKeyPrefix() + '/o';
        }
    }

    public PageReference save(){
        Weekly_Plan__c wpcInsert=new Weekly_Plan__c();
        List<Weekly_Plan__c> wpcList=[select Id from Weekly_Plan__c where OwnerId=:System.UserInfo.getUserId() and End_Date__c>:wpc.Start_Date__c and Start_Date__c<:wpc.End_Date__c];
        if(!wpcList.isEmpty()){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR
                                                        ,'该周计划的时间与其他周计划存在重叠'));
            if(!System.Test.isRunningTest()){
                return null;
            }
        }
        try{
        
            wpcInsert.Start_Date__c=wpc.Start_Date__c;
            wpcInsert.End_Date__c=wpc.End_Date__c;
            wpcInsert.RecordTypeId=rtId;
            Insert wpcInsert;
        }catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
                                     '操作出错：'+e.getMessage()));
            return null;
        }
        List<Event> taskIncomList=new List<Event>();
        System.debug('*** strjson: ' + strjson + '');
            List<Object> inJsonMap = (List<Object>)JSON.deserializeUntyped(strjson);
            for(Object obj : inJsonMap){
                Map<String,Object> objmap=(Map<String,Object>)obj;
                Event taskIncom=new Event();
                System.debug('*** objmap: ' + objmap + '');
                if(objmap.get('cam')+''!='')
                {
                    taskIncom.WhatId=Id.valueOf(String.valueOf(objmap.get('cam')));
                    taskIncom.Campaigns__c=Id.valueOf(String.valueOf(objmap.get('cam')));
                    taskIncom.RecordTypeId=rtHZId;
                }
                taskIncom.Subject=String.valueOf(objmap.get('sub'));
                taskIncom.StartDateTime    =DateTime.valueOf(''+objmap.get('startvisitdate')+':00');
                taskIncom.EndDateTime=DateTime.valueOf(''+objmap.get('endvisitdate')+':00');
                taskIncom.Week_Plan__c=wpcInsert.Id;
                taskIncom.FromWeeklyPlan__c=true;

                
                taskIncomList.add(taskIncom);

            }
            try{
                System.debug('*** taskIncomList: ' + taskIncomList + '');
                Insert taskIncomList;
                
            }catch(Exception e){
                delete wpcInsert;
                ApexPages.addMessage(
                new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
                return null;
            }


        return new PageReference('/'+wpcInsert.Id);
    }

    public PageReference cancle(){
        return new PageReference(tabUrl);
    }
}